// -*- C++ -*------------------------------------------------------------------
// $Id: LigandTest.cc,v 1.3 2007-10-26 14:55:16 biocomp Exp $

#include <GetArg.h>
#include <Spacer.h>
#include <vector3.h>
#include <PdbLoader.h>
#include <PdbSaver.h>
#include <IoTools.h>
#include <IntCoordConverter.h>
#include <LigandSet.h>

using namespace Biopool;

void sShowHelp()
{
  cout << "Ligand Test\n"
       << " Options: \n"
       << "\t-i <filename> \t\t Input PDB file\n"
       << "\t-m <distance> \t\t Max distance for ligand 'contact'\n"
       << "\n";
}

void sAddLine()
{
  cout << "-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-\n";
}

int main(int nArgs, char* argv[])
{ 
  if (getArg( "h", nArgs, argv))
    {
      sShowHelp();
      return 1;
    };

  string inputFile;
  double maxDist;

  getArg( "i", inputFile, nArgs, argv, "!");
  getArg( "m", maxDist, nArgs, argv, 4.0);

  if (inputFile == "!")
    {
      cout << "Missing file specification. Aborting. (-h for help)" << endl;
      return -1;
    }


  Spacer sp;
  ifstream inFile2(inputFile.c_str());
  if (!inFile2)
    ERROR("File not found.", exception);
  PdbLoader pl(inFile2);
  pl.setNoHAtoms();
  sp.load(pl);

  LigandSet ls;
  ls.load(pl);


  sAddLine();

  cout << "# of Ligands: " << ls.sizeLigand() <<  "\n";
  cout << "Adjacent residues:\n";

  for (unsigned int i = 0; i < ls.sizeLigand(); i++)
   if (ls.getLigand(i).isCommonMetal())
   {
      cout << " " << i << "  " << ls.getLigand(i).getType() << "\n";

      for (unsigned int j = 0; j < ls.getLigand(i).size(); j++)
      {
	  cout << "\t" << j << "  "<< ls.getLigand(i)[j].getType() << "   ";
	  
	  for (unsigned int k = 0; k < sp.sizeAmino(); k++)
	      for (unsigned int l = 0; l < sp.getAmino(k).size(); l++)
		  if (sp.getAmino(k)[l].distance(ls.getLigand(i)[j]) 
		      <= maxDist)
		      cout << "\t" << k << " (" 
			   << sp.getAmino(k)[l].getType() << ") ";
	  cout << "\n";
      }

   }

//    PdbSaver ps(cout);
//    sp.save(ps);
//    ls.save(ps);
//    ps.endFile();

  sAddLine();

  return 0;
}
